
/**
 * Contains all operations for the type management page..
 */
var typeview = {
	FORM_URL: null,
	TYPE_LIST: null,
	EDIT_TYPE: null,
	// name index for parent types
	nameIndex: { 0: '* Top Level Type *'},
	
	init: function() {
		this.FORM_URL = util.getURL('dbo_util_file')+'/type.jsp';
		this.EDIT_TYPE = new dbotype();
		this.fetchTopList();
		layout.leftcolumn.setTitle('Type List');
		layout.mainpanel.setTitle('New Type');
	},
	
	loadForm: function() {
		req = new util.ajax.Request(this.FORM_URL);
		req.setResponseHandler('typeview.displayForm');
		req.loadFile();	
	},
	
	displayForm: function(reqst) {
		layout.mainpanel.setContent(reqst.response);
		layout.mainpanel.setTitle('New Type');
		
		document.forms['typeform'].elements['parent'].value = typeview.parentLevel;
		document.getElementById('parentTypeDisplay').innerHTML = typeview.nameIndex[typeview.parentLevel];
	},
	
	populateForm: function(dbobj) {
		dbobj.toForm(document.forms['typeform']);
		layout.mainpanel.setTitle(dbobj.getProperty('name')+' - '+dbobj.getProperty('description'));
		
		document.forms['typeform'].elements['parent'].value = dbobj.getProperty('parent');
		document.getElementById('parentTypeDisplay').innerHTML = typeview.nameIndex[dbobj.getProperty('parent')];
	},

	// The level that invoked the list fetch
	parentLevel: 0,
	
	fetchTopList: function() {
		this.parentLevel = 0;
		this.fetchList({'parent':'0'});	
	}, 
	
	refreshList: function() {
		this.TYPE_LIST.list(this.LIST_PARAMS);	
	},
	
	expandList: function(tid) {
		document.getElementById('childCell-'+tid).style.display="block";
		
		anc = document.getElementById('expand-'+tid)
		anc.innerHTML = ' <img src="images/tree-open.png" align="absmiddle" border="0"/> ';
		anc.href = 'javascript:typeview.shrinkList('+tid+')';
		
		this.parentLevel = tid;
		
		this.fetchList({'parent':tid});	
	},
	
	shrinkList: function(tid) {
		document.getElementById('childCell-'+tid).style.display="none";
		
		anc = document.getElementById('expand-'+tid)
		anc.innerHTML = ' <img src="images/tree-closed.png" align="absmiddle" border="0"/> ';
		anc.href = 'javascript:typeview.expandList('+tid+')';
		
		tbl = document.getElementById('childTable-'+tid);
		while(tbl.childNodes.length > 0) tbl.removeChild(tbl.firstChild);
		
	},
	
	fetchList: function(params) {
		this.LIST_PARAMS = params;
		
		this.TYPE_LIST = new dbotype();
		this.TYPE_LIST.onReturn = 'typeview.listTypes';
		
		// add parameters..
		params['status'] = '1';
		params['orderBy'] = 'seq';
		
		this.TYPE_LIST.list(params);
	},
	
	listTypes: function(dbolist) {
		
		if (typeview.parentLevel == 0) {
			lcc = layout.leftcolumn.getContent();
			lcc.innerHTML = "";
			this.listtable = document.createElement("table");
			this.listtable.width="100%";
			lcc.appendChild(this.listtable);


		} else {
			this.listtable = document.getElementById('childTable-'+typeview.parentLevel);
		}

		while(this.listtable.childNodes.length > 0) this.listtable.removeChild(this.listtable.firstChild);
		
		rw = this.listtable.insertRow(-1);
		cl = rw.insertCell(-1);
		cl.innerHTML = '<a href="javascript:typeview.editType('+typeview.parentLevel+',0)">' +
						'	<img width="10" height="10"src="images/new.png" align="absmiddle" border="0"/>' +
						'	<b>&nbsp;New Type</b>' +
						'</a>';	

		arr = dbolist.getArray();
		if (arr) {
			for (o=0;o<arr.length;o++) {
				typeview.nameIndex[arr[o].getProperty('id')] = arr[o].getProperty('name');
				
				rw = this.listtable.insertRow(-1);
				cl = rw.insertCell(-1);
				cl.width = "100%";
				
				cl.innerHTML = '<a id="expand-' + arr[o].getProperty('id') + '" href="javascript:typeview.expandList(' + arr[o].getProperty('id') + ')">' +
									' <img src="images/tree-closed.png" align="absmiddle" border="0"/> ' +
								'</a>' +
								'<a href="javascript:typeview.editType('+arr[o].getProperty('parent')+','+arr[o].getProperty('id')+')">'+arr[o].getProperty('name')+'</a>';
				
				rw = this.listtable.insertRow(-1);
				cl = rw.insertCell(-1);
				cl.id = 'childCell-'+arr[o].getProperty('id');
				cl.style.display = 'none';
				
				tbl = document.createElement('table');
				cl.appendChild(tbl);
				rw = tbl.insertRow(-1);
				cl = rw.insertCell(-1);
				cl.style.width='5px';
				cl.style.backgroundImage = 'url(images/tree-nest.png)';
				cl.style.backgroundRepeat = 'repeat-y';
								
				cl = rw.insertCell(-1);
				
				cl.innerHTML = '<table id="childTable-'+arr[o].getProperty('id')+'"></table>';	
			}		
		}
		this.loadForm();
	},
	
	/**
	 * Edit type... or add new..
	 */
	editType: function(pid,tid) {
		frm = document.forms['typeform'];
		
		this.parentLevel = pid;
		
		this.EDIT_PE = dbotype();
		if (tid > 0) { 
			this.EDIT_TYPE.onReturn = 'typeview.populateForm';
			this.EDIT_TYPE.load({"id": tid});
		} else {
			this.loadForm();
		}
	},
	
	saveType: function() {
		frm = document.forms['typeform'];
		//if (util.form.validate(frm)) {
			this.EDIT_TYPE.fromForm(frm);
			this.EDIT_TYPE.onReturn = "typeview.refreshList";
			this.EDIT_TYPE.save();
		//}
	},

	deleteType: function() {
		frm = document.forms['typeform'];
		this.EDIT_TYPE.fromForm(frm);
		if (parseInt(this.EDIT_TYPE.getProperty('id')) == 0) {
			return;
		}
		this.EDIT_TYPE.onReturn = "typeview.refreshList";
		this.EDIT_TYPE.save({'status':99});
	}


}